clc;clear;clearvars;
load('iris_X.mat', 'iris');
x = iris;
load('iris_Y.mat', 'iris');
y = iris;
% num表示有多少个类标签
num = 3;
n = size(x, 1);
for k = 1 : 10
    test_y = zeros(n ,1);
    for i = 1 : n
        % 在样本集中选择一个样本作为测试集，其他样本作为训练集
        test_x = x(i, :);
        if i == 1
            ind = i + 1 : n;
            test_y(i, :) = knn(x(ind, :), y(ind, :), test_x, k, num);
        else
            ind = [1 : i - 1, i + 1 : n];
            test_y(i, :) = knn(x(ind, :), y(ind, :), test_x, k, num);
        end
    end
    err_num = sum(test_y ~= y);
    err_rate = err_num / n;
    fprintf("iris total: %d k: %d err_num: %d err_rate: %.4f\n",n,k,err_num,err_rate);
end